<?php
/**
 * 
 * Контроллер контактов
 * @author andrew
 *
 */
class Pages_ContactsController extends Zend_Controller_Action {
	
	/**
	 * 
	 * Просмотр списка контактов
	 */
	public function indexAction() {
		$contacts = Doctrine_Core::getTable('Pages_Model_Contacts')->findAll();
		
		$this->view->contacts = $contacts;
	}
	/**
	 * 
	 * Создание контакта
	 */
	public function createAction() {
		$form = new Pages_Form_Contacts();
		$form->setAction($this->view->url(array(),'pages_contacts_create'));
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$contact = new Pages_Model_Contacts();
			
			foreach ($values['name'] as $lang=>$value) {
				
				$contact->Translation[$lang]->name = $value;
				$contact->Pages_Model_Geocoord->Translation[$lang]->name = $value;	
			}
			
			foreach ($values['title'] as $lang=>$value) {
				if($value == '') $value = null;
				$contact->Translation[$lang]->title = $value;	
			}
			
			foreach ($values['content'] as $lang=>$value) {
				$contact->Translation[$lang]->content = $value;	
			}
			
			$contact->published = $values['published'];
			
			if($values['is_main'] == 1) {
				$mainContact = Doctrine_Core::getTable('Pages_Model_Contacts')->findOneByIs_main(true);
				if($mainContact) {
					$mainContact->is_main = false;
					$mainContact->save();
				}
			}
			$contact->is_main = $values['is_main'];
			$contact->Pages_Model_Geocoord->coord = $values['geocoord'];
			$contact->save();
			
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Contact has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'pages_contacts_index'));
		}
		
		$this->view->form = $form;
	}
	/**
	 * 
	 * Обновление контакта
	 * @throws Zend_Controller_Action_Exception
	 */
	public function updateAction() {
		$id = $this->_request->getParam('id',0);
		
		$contact = Doctrine_Core::getTable('Pages_Model_Contacts')->find($id);
		
		if(!$id || !$contact)
			throw new Zend_Controller_Action_Exception('Contact not found!', 404);
			
		$populate = array(
			'published' => $contact->published,
			'is_main' => $contact->is_main,
			'geocoord' => $contact->Pages_Model_Geocoord->coord
		);
		
		$locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
		
		foreach ($locales as $lang=>$value) {
			$populate['name'][$lang] = $contact->Translation[$lang]->name;
			$populate['title'][$lang] = $contact->Translation[$lang]->title;
			$populate['content'][$lang] = $contact->Translation[$lang]->content;
		}
		
		$form = new Pages_Form_Contacts();
		$form->setAction($this->view->url(array('id' => $contact->id),'pages_contacts_update'));
		
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
						
			foreach ($values['name'] as $lang=>$value) {
				
				$contact->Translation[$lang]->name = $value;
				$contact->Pages_Model_Geocoord->Translation[$lang]->name = $value;	
			}
			
			foreach ($values['title'] as $lang=>$value) {
				if($value == '') $value = null;
				$contact->Translation[$lang]->title = $value;	
			}
			
			foreach ($values['content'] as $lang=>$value) {
				$contact->Translation[$lang]->content = $value;	
			}
			
			$contact->published = $values['published'];
			
			if($values['is_main'] == 1) {
				$mainContact = Doctrine_Core::getTable('Pages_Model_Contacts')->findOneByIs_main(true);
				if($mainContact) {
					$mainContact->is_main = false;
					$mainContact->save();
				}
			}
			$contact->is_main = $values['is_main'];
			$contact->Pages_Model_Geocoord->coord = $values['geocoord'];
			$contact->save();
			
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Contact has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'pages_contacts_index'));
		} else {
			$form->populate($populate);
		}
	}
	
	/**
	 * 
	 * Удаление контакта
	 * @throws Zend_Controller_Action_Exception
	 */
	public function deleteAction() {
		$id = $this->_request->getParam('id',0);
		
		$contact = Doctrine_Core::getTable('Pages_Model_Contacts')->find($id);
		
		if(!$id || !$contact)
			throw new Zend_Controller_Action_Exception('Contact not found!', 404);
			
		$contact->delete();
		
		$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Contact has been deleted successfully!'));
		return $this->_redirect($this->view->url(array(),'pages_contacts_index'));
	}
}